const http = require('http')
const fs = require('fs')
const server = http.createServer((req, res)=>{
  
  const {url, method, headers} = req
  if(url === '/' && method==='GET'){
    fs.readFile('./index.html',(err, data)=>{
      if(err){
        res.writeHead(500, {'Content-Type':'text/plain'})
        res.end('服务器出错')
      }
      res.statusCode = 200
      res.setHeader('Content-Type', 'text/html')
      res.end(data)
    })
  } else if(url === '/users' && method === 'GET'){
    res.writeHead(200, {'Content-Type': 'application/json'})
    res.end(JSON.stringify({name: 'xia'}))
  } else if(method==='GET' && headers.accept.indexOf('image/*') !== -1){
    fs.createReadStream('./img' + url).pipe(res)
  }
})
server.listen(3000)

//跨域
function cors(res){
  res.setHeader("Content-Type", "application/json")
  res.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:8080")
  res.setCookie("cookie1", '123')
}

const router = []
router.push({
  url: '/user',
  method: 'GET',
  cb:(req, res)=>{}
})




